﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ParserTree;

namespace Compiler_package
{
    public partial class Form1 : Form
    {
        private bool parseResult;
        public Form1()
        {
            InitializeComponent();
        }

        private void BtnScanner_Click(object sender, EventArgs e)
        {
            Scanner.Scanner s = new Scanner.Scanner();
            s.program = this.rtxtInput.Text;
            s.Scan();
            string allTokens = "";
            for (int i = 0; i < s.tokenList.Count; i++)
            {
                allTokens += "Token number {" + i + "}:" + s.tokenList[i].getToken() + "\n";

            } MessageBox.Show(allTokens);
        }

        private void BtnParse_Click(object sender, EventArgs e)
        {
            Scanner.Scanner s = new Scanner.Scanner();
            s.program = this.rtxtInput.Text;
            s.Scan();

            parser.Parser newpa = new parser.Parser();
            parseResult = newpa.parse(s);
            MessageBox.Show("the input is " + parseResult);


        }


        private void BtnParseTree_Click(object sender, EventArgs e)
        {
            Scanner.Scanner s = new Scanner.Scanner();
            s.program = this.rtxtInput.Text;
            s.Scan();
            parser.Parser newpa = new parser.Parser();
            parseResult = newpa.parse(s);

            if (parseResult)
            {
                Scanner.Scanner s2 = new Scanner.Scanner();
                s2.program = this.rtxtInput.Text;
                s2.Scan();
                ParserTree.ParserTree newParseTree = new ParserTree.ParserTree();
                TreeNode1 Node = newParseTree.parse(s2);
                ParseTreeForm form = new ParseTreeForm();
                form.SetInfo(Node.tNode, this.rtxtInput.Text);
                form.Show();
                
            }
            else
            {
                MessageBox.Show("There is a parse error \"parse Tree\" can not be display");
            }
        }
    }
}
